package com.smh.demo1.a20;

import com.smh.demo1.a20.pojo.Bean1;
import com.smh.demo1.a20.pojo.Bean2;
import org.springframework.core.GenericTypeResolver;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author shiminghui
 * @date 2025/5/1 18:04
 * @description: 尝试获取泛型类型
 */
public class TestGetGenericType {
    public static void main(String[] args) {
        // 1.使用jdk的方法
        Type type = Bean1.class.getGenericSuperclass();
        if (type instanceof ParameterizedType parameterizedType) {
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            System.out.println(actualTypeArguments[0]);
        }

        // 2.使用spring的方法
        Class<?>[] classes = GenericTypeResolver.resolveTypeArguments(Bean1.class, Bean2.class);
        for (Class<?> aClass : classes) {
            System.out.println(aClass);
        }

    }
}
